﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using RN.ERP.Entity.QD;
using System.Data.Entity;
using RN.ERP.Entity.ZBDLRY;
using RN.ERP.DAL.Context;

namespace RN.ERP.ZbdlryDAL
{
    public class ProjectDAL
    {
        private ZBDL_Context zbdlcontext = new ZBDL_Context();

        public DataTable GetJrkbProject(string areaCode, int codeClass)
        {
            DataTable dtResult = new DataTable();
            using (SqlConnection connection = new SqlConnection(zbdlcontext.Database.Connection.ConnectionString))
            {
                connection.Open();
                SqlCommand command = new SqlCommand();
                command.Connection = connection;
                command.CommandText = @"select (select top 1 DatePBStart from zb_ztbMBFileHeader where pID=p.id and DatePBStart between dateadd(day,0,convert(char(10),getdate(),120)) and dateadd(day,1,convert(char(10),getdate(),120)) )as kbdate ,p.ProjectName,
 (select top 1 PBRoomInfo from zb_ztbMBFileHeader where pID=p.id and DatePBStart between dateadd(day,0,convert(char(10),getdate(),120)) and dateadd(day,1,convert(char(10),getdate(),120)) ) as projectroom,a.title projectkind
 from zb_ProjectInfo p 
join a_CodeClass a on a.id=p.bCodeClass
where p.FlowCode2 like '1%' and p.AreaCode like @areaCode and p.bCodeClass=@bCodeClass and
exists(select 1 from zb_ztbMBFileHeader where DatePBStart between dateadd(day,0,convert(char(10),getdate(),120)) and dateadd(day,1,convert(char(10),getdate(),120)) and pID=p.ID)";
                command.CommandType = CommandType.Text;
                command.Parameters.AddRange(new SqlParameter[] { 
                    new SqlParameter("areaCode",areaCode+"%"),
                    new SqlParameter("bCodeClass",codeClass)
                });
                SqlDataAdapter adapter = new SqlDataAdapter(command);
                adapter.Fill(dtResult);
            }
            return dtResult;
        }
    }
}
